package com.packaging.dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.packaging.bean.PcBox;
import com.packaging.bean.PcPanel;
import com.packaging.hibernate.HibernateSessionFactory;

public class PackBoxDao {
	private Session session;
	private Transaction tr;
	public List getBoxLts(){
		try{
			session = HibernateSessionFactory.getSession();
			return session.createQuery("from PcBox").list();
		}catch(Exception e) {
			e.printStackTrace();
			return null;
		}finally {
			HibernateSessionFactory.closeSession();
		}
	}
	
	public boolean saveBox(PcBox pcBox) {
		try {
			session = HibernateSessionFactory.getSession();
			tr = session.beginTransaction();
			session.save(pcBox);
			tr.commit();
			return true;
		}catch(Exception e) {
			e.printStackTrace();
			if(tr!=null){
				tr.rollback();
			}
			return false;
		}finally {
			HibernateSessionFactory.closeSession();
		}
	}
	
	public static void main(String[] args) {
		PackBoxDao pbDao = new PackBoxDao();
		
//		PcPanel p1 = new PcPanel();
//		p1.setPanel_length(1.11);
//		p1.setPanel_width(1.11);
//		p1.setPanel_high(1.11);
//		p1.setPanel_type(1);
//		
//		PcPanel p2 = new PcPanel();
//		p2.setPanel_length(1.2);
//		p2.setPanel_width(1.2);
//		p2.setPanel_high(1.2);
//		p2.setPanel_type(2);
//		
//		PcBox pcBox = new PcBox();
//		pcBox.setBox_name("Box1");
//		pcBox.setBox_num("box1");
//		pcBox.setBox_CM("11.11");
//		
//		pcBox.getPcPanels().add(p1);
//		pcBox.getPcPanels().add(p2);
//		
//		pbDao.saveBox(pcBox);
		
//		List pbs = pbDao.getBoxLts();
//		Iterator it = pbs.iterator();
//		while(it.hasNext()) {
//			PcBox pb = (PcBox) it.next();
//			System.out.println("ID=>"+pb.getBox_id());
//			if(pb.getPcPanels()!=null && pb.getPcPanels().size()>0){
//				Iterator i = pb.getPcPanels().iterator();
//				while(i.hasNext()) {
//					PcPanel pp = (PcPanel) i.next();
//					System.out.println("l=>"+pp.getPanel_length());
//					System.out.println("w=>"+pp.getPanel_width());
//					System.out.println("h=>"+pp.getPanel_high());
//				}
//			}
//		}
	}
}
